package com.ityj.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ityj.yygh.cmn.client.DictFeignClient;
import com.ityj.yygh.enums.DictEnum;
import com.ityj.yygh.model.user.Patient;
import com.ityj.yygh.user.mapper.PatientMapper;
import com.ityj.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAll(Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(queryWrapper);
        if (patientList == null) {
            return null;
        }
        patientList.forEach(this::populateFields);
        return patientList;
    }

    @Override
    public void savePatient(Patient patient) {
        baseMapper.insert(patient);
    }

    @Override
    public Patient findById(Long id) {
        Patient patient = baseMapper.selectById(id);
        this.populateFields(patient);
        return patient;
    }

    @Override
    public void removeById(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void updatePatient(Patient patient) {
        baseMapper.updateById(patient);
    }

    private void populateFields(Patient patient) {
        // 根据证件类型编码，获取证件类型具体值
        String certificatesTypeName = dictFeignClient.getDictName(patient.getCertificatesType(), DictEnum.CERTIFICATES_TYPE.getDictCode());
        String contactsCertificatesTypeString =
                dictFeignClient.getDictName(patient.getContactsCertificatesType(), DictEnum.CERTIFICATES_TYPE.getDictCode());
        String provinceString = dictFeignClient.getDictName(patient.getProvinceCode());
        String cityString = dictFeignClient.getDictName(patient.getCityCode());
        String districtString = dictFeignClient.getDictName(patient.getDistrictCode());
        Map<String, Object> param = patient.getParam();
        param.put("certificatesTypeString", certificatesTypeName);
        param.put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        param.put("provinceString", provinceString);
        param.put("cityString", cityString);
        param.put("districtString", districtString);
        param.put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
    }

}
